package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.sysmg.security.model.UserModel;
import com.e3.sysmg.security.model.UserQC;
import com.e3.template.tgf.TGFStateMachine;
import com.e3.web.core.client.GridListener;
import com.e3.web.core.client.QueryListener;
import com.e3.web.core.model.SuperQC;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.e3.web.widget.client.MyRefDialog;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;

public class UserRefDialog extends MyRefDialog<UserModel> implements QueryListener<UserModel>,GridListener<UserModel>{
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private UserModel selectedModel;
	
	private ContentPanel contentPanel = new ContentPanel(new BorderLayout());
	
	private ToolBar toolbar = new ToolBar();
	
	private MyButton queryButton = new MyButton(UserStateMachine.QUERY_ACTION);
	
	private UserGridPanel gridPanel;
	
	private UserQueryDialog queryDialog;
		
	@Override
	protected void onRender(Element parent, int pos) {
		
		super.onRender(parent, pos);
		
		this.setSize(600, 450);
		this.setHeading("");
		setLayout(new FitLayout());
		
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.CENTER);
		contentPanel.add(getGridPanel(),bld);
		
		initToolBar();
		
		contentPanel.setHeaderVisible(false);
		this.add(contentPanel);
	}
	
	@Override
	public void onRefWindowShow() {
		if(selectedModel!=null){
			List<UserModel> models = new ArrayList<UserModel>(1);
			models.add(selectedModel);
			getGridPanel().setGridValues(models);
		}
	}

	@Override
	public UserModel getRefValue() {
		return selectedModel;
	}

	@Override
	public void setRefValue(UserModel value) {
		selectedModel = value;
	}
	
	private void onBtnMnuEvent(BaseAction action) {
		if(action == TGFStateMachine.QUERY_ACTION){
			onQuery();
		}
	}
	
	private void onQuery(){
		getQueryDialog().setVisible(true);
	}

	private void initToolBar(){
		queryButton.addSelectionListener(btnSelectListener);
		toolbar.add(queryButton);
		contentPanel.setTopComponent(toolbar);
	}
	
	public UserGridPanel getGridPanel(){
		if(gridPanel == null){
			gridPanel = new UserGridPanel();
			gridPanel.addGridListener(this);
		}
		return gridPanel;
	}

	
	protected UserQueryDialog getQueryDialog() {
		if(queryDialog == null){
			queryDialog = new UserQueryDialog();
			queryDialog.addQueryListener(this);
		}
		return queryDialog;
	}
	
	public void onQuerySuccessed(UserQC qc,List<UserModel> models) {
		getGridPanel().setGridValues(models);
	}

	public void onGridSelectionChanged(UserModel model) {
		selectedModel = model;
	}

	public void onGridRowDoubClicked(UserModel model) {
		onOk();
	}
	
	public List<UserModel> getSelectedModels(){
		return null;
	}

	public void onQuerySuccessed(SuperQC qc, List<UserModel> results) {
		
	}

}
